package com.nb.hdj.testCase.configurationModule;

import com.nb.hdj.baseClass.SeleniumUtil;
import com.nb.hdj.data.HtConfigurationEnum;
import com.nb.hdj.model.ConfigurationManagement;
import com.nb.hdj.util.YamlReadUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import java.util.Map;

/**
 * @Description: TODO
 * @Author: hedongjia
 * @Date: 2021/12/16 下午6:47
 * @Version: v1.0
 */
public class ConfigurationCaseBase {
    static YamlReadUtils yamlReadUtils=new YamlReadUtils();
    static SeleniumUtil seleniumUtil=new SeleniumUtil();
    static Map<String, String> configurationMap=yamlReadUtils.getMapValueByKey("Configuration", "htconfiguration.yaml");
    static ConfigurationManagement cm=new ConfigurationManagement();

    /**
     * 配置管理 子菜单查看方法封装
     * @param driver
     * @param childMenuElement 子模块元素
     * @param firstNameElement 第一行名称内容
     * @return
     */
    public boolean viewBase(WebDriver driver,String childMenuElement,String  firstNameElement){
        try {
            //点击内容发布频道
            seleniumUtil.elementClick(driver, By.xpath(childMenuElement));
            seleniumUtil.sleep(1500);
            if(!seleniumUtil.elementTextIsEqual(driver,By.xpath(firstNameElement),"")){
                return true;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 配置管理 子菜单新增方法封装
     * @param driver
     * @param cm 配置管理实体类
     * @return
     */
    public boolean insertBase(WebDriver driver,ConfigurationManagement cm){
        try {
            seleniumUtil.sleep(800);
            //点击新增
            seleniumUtil.elementClick(driver, By.xpath(HtConfigurationEnum.CONFIGURATION_Content_Insert_Button_Element.getValue()));
            //调用新增修改操作 封装
            insertAndUpdateBase(driver,cm);
            if(seleniumUtil.elementTextIsEqual(driver,By.xpath
                    (HtConfigurationEnum.CONFIGURATION_Content_First_Name_Element.getValue()),cm.getClassName())){
                return true;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 配置管理 子菜单更改方法封装
     * @param driver
     * @return
     */
    public boolean updateBase(WebDriver driver,ConfigurationManagement cm){
        try {
            //点击修改
            seleniumUtil.elementClick(driver, By.xpath
                    (HtConfigurationEnum.CONFIGURATION_Content_Update_Button__Element.getValue()));
            //调用新增修改操作 封装
            insertAndUpdateBase(driver,cm);
            if(seleniumUtil.elementTextIsEqual(driver,By.xpath
                    (HtConfigurationEnum.CONFIGURATION_Content_First_Name_Element.getValue()),cm.getClassName())){
                return true;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 配置管理 子菜单删除方法封装
     * @param driver
     * @return
     */
    public boolean deleteBase(WebDriver driver,ConfigurationManagement cm){
        try {
            //点击删除
            seleniumUtil.elementClick(driver, By.xpath
                    (HtConfigurationEnum.CONFIGURATION_Content_Delete_Button__Element.getValue()));
            //点击确定
            seleniumUtil.elementClick(driver, By.xpath
                    (HtConfigurationEnum.CONFIGURATION_Content_Delete_Confirm__Element.getValue()));
            seleniumUtil.sleep(1000);
//            String aa=seleniumUtil.getElementText(driver,By.xpath
//                    (HtConfigurationEnum.CONFIGURATION_Content_First_Name_Element.getValue()));
            if(!seleniumUtil.elementTextIsEqual(driver,By.xpath
                    (HtConfigurationEnum.CONFIGURATION_Content_First_Name_Element.getValue()),cm.getClassName())){
                return true;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }



    /**
     * 新增修改操作 封装
     * @param driver
     */
    public void insertAndUpdateBase(WebDriver driver, ConfigurationManagement cm){
        try {
            //输入分类名称
            seleniumUtil.elementClickAndClearAndInput(driver,By.xpath
                    (HtConfigurationEnum.CONFIGURATION_Content_Name_Input_Element.getValue()),cm.getClassName());
            //输入排序顺序
            seleniumUtil.elementClickAndClearAndInput(driver,By.xpath
                    (HtConfigurationEnum.CONFIGURATION_Content_Sort_Input_Element.getValue()),cm.getSortNumber());
            //点击显示状态下拉框
            seleniumUtil.elementClick(driver,By.xpath
                    (HtConfigurationEnum.CONFIGURATION_Content_Show_Selection_Element.getValue()));
            //选择显示
            seleniumUtil.elementClick(driver,By.xpath
                    (HtConfigurationEnum.CONFIGURATION_Content_Is_Show__Element.getValue()));
            //点击确定
            seleniumUtil.elementClick(driver, By.xpath
                    (HtConfigurationEnum.CONFIGURATION_Content_Confirm_Button__Element.getValue()));
            seleniumUtil.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }




}
